<?php

namespace app\admin\controller;
use think\Db;
use think\Validate;
use think\Controller;

class Staff extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page =1 ,$pageSize = 10,$ins_id,$search =[])
    {
        if(!$ins_id){
            return ['code'=>1,'msg'=>'参数错误'];
        }
        $search['ins_id'] = $ins_id;
        $data = model('Common/Staff')->getList($page,$pageSize,$search);
        return ['code'=>0,'msg'=>'数据获取成功','data'=>$data];
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save($info=[])
    {
        $result = $this->validate(
            $info,
            'app\common\validate\Staff.add'
        );

        if (true !== $result) {
            return ['code'=>1,'msg'=>$result];
        }
        Db::startTrans();
        //判断手机号是否被注册
        $key = model('common/Staff')->get_one_from_phone($info['phone']);
        if($key){
            return ['code'=>3,'msg'=>'该手机号已被注册'];
        }
        //插入人员
        $r[] = model('common/Staff')->add_one($info);
        //增加人数
        $r[] = model('common/Institution')->Inc_count($info['ins_id']);
        if(!in_array(false,$r)){
            Db::commit();
            return ['code'=>0,'msg'=>'添加成功'];
        }else{
            Db::rollback();
            return ['code'=>2,'msg'=>'添加失败'];
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $info = model('Common/staff')->get_one($id,['id','name','phone','duties','authority']);
        return ['code'=>0,'msg'=>'数据获取成功','data'=>$info];
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id,$info=[])
    {
        if(!$id){
            return ['code'=>1,'msg'=>'参数错误'];
        }
        $result = $this->validate(
            $info,
            'app\common\validate\Staff.edit'
        );
        if($info['password']){
            $validate   = Validate::make(['password'=>'require|alphaDash|length:6,16'],[

                'password.require'=>'请输入密码',
                'password.alphaDash'=>'密码只能包含数字字母_-',
                'password.length'=>'密码长度只能在6到16位',

            ]);
            $result1 = $validate->check(['password'=>$info['password']]);
            if (true !== $result1) {
                return ['code'=>5,'msg'=>$validate->getError()];
            }
            $info['password'] = md5($info['password']);
        }else{
            unset($info['password']);
        }
        if (true !== $result) {
            return ['code'=>2,'msg'=>$result];
        }

        //判断手机号是否被注册
        $key = model('common/Staff')->get_one_from_phone_unset_id($id,$info['phone']);
        if($key){
            return ['code'=>3,'msg'=>'该手机号已被注册'];
        }



        $r = model('Common/Staff')->edit($id,$info);
        if($r){
            return ['code'=>0,'msg'=>'修改成功'];
        }else{
            return ['code'=>3,'msg'=>'修改失败'];
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(!$id){
            return ['code'=>1,'msg'=>'参数错误'];
        }
        $user = model('common/staff')->get_one($id);
        DB::startTrans();
        //删除员工
        $r[] = model('common/staff')->del($id);
        //减少机构人员
        $r[] = model('common/institution')->Dec_count($user['ins_id']);
        if(!in_array(false,$r)){
            Db::commit();
            return ['code'=>0,'msg'=>'删除成功'];
        }else{
            Db::rollback();
            return ['code'=>2,'msg'=>'删除失败'];
        }
    }
}
